// 记录以点击红包的oid
var arr = [];
// 记录监测红包的次数
var count = 0;
// 记录普通红包的点击次数
var clickcount = 0;
// 记录猜拳红包的点击次数
var clickcount1 = 0;
function wait(delay) {
  return new Promise(resolve => setTimeout(resolve, delay));
}
const getData = (oId, gesture) => {
  console.log(oId, gesture);
  $.ajax({
    url: '/chat-room/red-packet/open',
    type: 'post',
    data: JSON.stringify({
      oId,
      gesture,
      dice: {}
    }),
    dataType: "json",
    success: (res) => {
      console.log(res);
    },
    error: (exp) => {
      console.log(exp);
    },
  })
}
function shuffle(array) {
  for (let i = array.length - 1; i > 0; i--) {
    let j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
  }
  return array;
}

function getRandomNumber(arr) {
  var randomIndex = Math.floor(Math.random() * arr.length);
  return arr[randomIndex];
}

setInterval(async function () {
  // count++;
  // console.log("检测了:"+count+"次")
  // 判断红包是否存在，判断红包的类型，记录红包的oid防止重复点击
  if (document.querySelector('div.hongbao__item') != null) {
    if (document.querySelector('div.hongbao__item').getAttribute('onclick') !== null) {
      if (document.querySelector('div.hongbao__item').getAttribute('onclick').indexOf("'") != -1) {
        if (arr.filter(val => val == document.querySelector('div.hongbao__item').getAttribute('onclick').split("'")[1]).length === 0) {
          arr.push(document.querySelector('div.hongbao__item').getAttribute('onclick').split("'")[1]);
          await wait(2000);
          console.log(document.querySelector('div.hongbao__item').getAttribute('onclick'))
          getData(document.querySelector('div.hongbao__item').getAttribute('onclick').split("'")[1],0)
          clickcount++;
          console.log("普通红包点击了:" + clickcount + "次")
        }
      } else {
        if (arr.filter(val => val == document.querySelector('div.hongbao__item').getAttribute('onclick').split('(')[1].split(')')[0]).length === 0) {
          arr.push(document.querySelector('div.hongbao__item').getAttribute('onclick').split('(')[1].split(')')[0]);
          await wait(2000);
          console.log(document.querySelector('div.hongbao__item').getAttribute('onclick'))
          getData(document.querySelector('div.hongbao__item').getAttribute('onclick').split('(')[1].split(')')[0],0)
          clickcount++;
          console.log("普通红包点击了:" + clickcount + "次")
        }
      }
    } else {
      if (document.querySelector('div.hongbao__item').children[document.querySelector('div.hongbao__item').children.length - 1].children[1].textContent.trim() < 100) {
        if (arr.filter(val => val == document.querySelector('div.hongbao__item').children[0].children[[0, 1, 2][Math.floor(Math.random() * 3)]].getAttribute("onclick").split(",")[0].split("(")[3]).length === 0) {
          arr.push(document.querySelector('div.hongbao__item').children[0].children[[0, 1, 2][Math.floor(Math.random() * 3)]].getAttribute("onclick").split(",")[0].split("(")[3]);
          await wait(2000);
          console.log(document.querySelector('div.hongbao__item').children[0].children[[0, 1, 2][Math.floor(Math.random() * 3)]].getAttribute("onclick"))
          getData(document.querySelector('div.hongbao__item').children[0].children[[0, 1, 2][Math.floor(Math.random() * 3)]].getAttribute("onclick").split(",")[0].split("(")[3],shuffle([2, 1, 0])[getRandomNumber(shuffle([1,0,2]))])
          clickcount1++;
          console.log("猜拳红包点击了:" + clickcount1 + "次")
        }
      }
    }
  }
});
